/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import java.lang.reflect.Constructor;
import java.util.logging.Logger;

import l1j.server.server.IdFactory;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.Instance.L1EffectInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_NPCPack;
import l1j.server.server.templates.L1Npc;

// Referenced classes of package l1j.server.server.model:
// L1EffectSpawn

public class L1EffectSpawn {
	private static final Logger _log = Logger.getLogger(L1EffectSpawn.class
			.getName());

	private static L1EffectSpawn _instance;

	private Constructor _constructor;

	public L1EffectSpawn() {
	}

	public static L1EffectSpawn getInstance() {
		if (_instance == null) {
			_instance = new L1EffectSpawn();
		}
		return _instance;
	}

	public void spawnEffect(int gfxId, int time, int locX, int locY, short mapId) {
		L1Npc l1npc = NpcTable.getInstance().getTemplate(81171);
		try {
			if (l1npc != null) {
				String s = l1npc.getImpl();
				_constructor = Class.forName(
						(new StringBuilder()).append(
								"l1j.server.server.model.Instance.").append(s)
								.append("Instance").toString())
						.getConstructors()[0];
				Object obj[] = { l1npc };
				L1EffectInstance effect = (L1EffectInstance) _constructor
						.newInstance(obj);
				effect.setId(IdFactory.getInstance().nextId());
				effect.setGfxId(gfxId);
				effect.setX(locX);
				effect.setY(locY);
				effect.setHomeX(locX);
				effect.setHomeY(locY);
				effect.setHeading(0);
				effect.setMap(mapId);
				L1World.getInstance().storeObject(effect);
				L1World.getInstance().addVisibleObject(effect);

				for (L1PcInstance pc : L1World.getInstance().getVisiblePlayer(effect)) {
					effect.addKnownObject(pc);
					pc.addKnownObject(effect);
					pc.sendPackets(new S_NPCPack(effect));
					pc.broadcastPacket(new S_NPCPack(effect));
				}
				L1NpcDeleteTimer timer = new L1NpcDeleteTimer(effect, time);
				timer.begin();
			}
		} catch (Exception exception) {
		}
	}
}
